package com.briup.jz.service.impl;


import com.briup.jz.bean.Address;
import com.briup.jz.bean.AddressExample;
import com.briup.jz.bean.extend.AddressExtend;
import com.briup.jz.dao.AddressMapper;
import com.briup.jz.dao.extend.AddressExtendMapper;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: jz
 * @description: 地址业务实现类
 * @author: catmilko
 * @create: 2020/6/12 15:58
 **/
@Service
public class AddressServiceImpl implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private AddressExtendMapper addressExtendMapper;

    @Override
    public List<Address> findAll() {
        AddressExample example = new AddressExample();
        return addressMapper.selectByExample(example);
    }

    @Override
    public Address findById(long id) {
        return addressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(long id) throws CustomerException {

        Address address = addressMapper.selectByPrimaryKey(id);
        if(address == null){
            throw  new CustomerException("删除失败，要删除的数据不存在");
        }
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Address> findByUserId(long userId) {
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return addressMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(Address address) throws CustomerException {
        if(address.getId() != null){
            addressMapper.updateByPrimaryKey(address);
        } else {
            addressMapper.insert(address);
        }
    }

    @Override
    public List<AddressExtend> findAllWithUser() {
        return addressExtendMapper.selectAllWithUser();
    }


}
